package com.jtjrenren.android.taxi.passenger.core.wallect.jindou;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jtjrenren.android.taxi.passenger.core.adapter.MaterialListAdapter;
import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.base.BaseActivity;
import com.jtjrenren.android.taxi.passenger.bean.api.ApiMaList;
import com.jtjrenren.android.taxi.passenger.manger.AppManger;
import com.jtjrenren.android.taxi.passenger.manger.api.ApiWorks;
import com.jtjrenren.android.taxi.passenger.utils.InitView;
import com.jtjrenren.android.taxi.passenger.utils.ModuleUtils;
import com.library.weidget.AutoLoadRecyclerView;
import com.jtjrenren.android.taxi.passenger.utils.ActivityIntentUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wdl on 2016/1/27.
 */
public class JinDouActivity extends BaseActivity {
    @BindView(R.id.wallect_jindou_count)
    TextView tv_jindou;
    @BindView(R.id.nocontent)
    LinearLayout ll_noncontent;
    @BindView(R.id.wallect_jindou_matrallist)
    AutoLoadRecyclerView rcy_list;
    @BindView(R.id.title)
    TextView tv_title;

    private MaterialListAdapter materialListAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.aty_wallect_jindou;
    }

    @Override
    public void init() {
        tv_title.setText("我的金豆");
        tv_jindou.setText(AppManger.getInstance().user.getCredits());

        InitView.initVritalReclyView(this, rcy_list);
        materialListAdapter = new MaterialListAdapter(this);
        rcy_list.setAdapter(materialListAdapter);

        getMaterialList();
    }

    @OnClick(R.id.title_left)
    public void clickBack(){
        finish();
    }

    @OnClick(R.id.wallect_jindou_history)
    public void clickHistory(){
        startActivity(new Intent(this,JinDouHistoryActivity.class));
    }

    @OnClick(R.id.wallect_jindou_desc)
    public void clickJdDesc(){
        ActivityIntentUtils.showHtmlActivity(this, ModuleUtils.getHtmlUrl(getString(R.string.html_bs_jddesc)), getString(R.string.huoqujindou), true);
    }


    /**
     * 是否有数据来显示页面
     *
     * @param isNodata
     */
    private void isNotDataShow(boolean isNodata) {
        if (isNodata) {
            ll_noncontent.setVisibility(View.VISIBLE);
            rcy_list.setVisibility(View.GONE);
        } else {
            ll_noncontent.setVisibility(View.GONE);
            rcy_list.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 从服务器获取商品列表
     */
    private void getMaterialList() {
        showWaittingDialog(null, true);
        ApiWorks.getMaterialList(new ApiWorks.ResponseListener<ApiMaList>() {
            @Override
            public void onResponse(ApiMaList apiMaList) {
                dismissWaittingDialog();
                if (apiMaList != null && apiMaList.getMaterialList() != null && apiMaList.getMaterialList().size() > 0) {
                    isNotDataShow(false);
                    List<ApiMaList.MaterialListEntity> materialList = apiMaList.getMaterialList();
                    materialListAdapter.replace(materialList);
                } else {
                    isNotDataShow(true);
                }
            }
        });
    }

}
